package com.bocai.yuweiwangluo.presenter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.adapter.TalkDetailListAdapter;
import com.bocai.yuweiwangluo.base.BaseView;
import com.bocai.yuweiwangluo.bean.BaseBean;
import com.bocai.yuweiwangluo.bean.TalkDetailBean;
import com.bocai.yuweiwangluo.net.MyOkHttpClient;
import com.bocai.yuweiwangluo.utils.ImageLoaderUtil;
import com.bocai.yuweiwangluo.utils.KeyBoardUtils;
import com.bocai.yuweiwangluo.utils.SharePrefencesUtil;
import com.bocai.yuweiwangluo.utils.TimeUtils;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.bocai.yuweiwangluo.viewModel.ITalkDetailView;
import com.google.gson.Gson;


/**
 * Created by admin on 2015/12/16.
 */
public class TalkDetailPresenter
{
    private TalkDetailBean talkDetailBean;
    private static final int RESPONSE_SUCCEED = 1;
    private static final int RESPONSE_FAILED = 2;
    private ITalkDetailView iTalkDetailView;
    private Context context;
    private BaseView baseView;
    private int requstType;
    private BaseBean baseBean;
    private TalkDetailListAdapter talkDetailListAdapter;
    public TalkDetailPresenter(Context context, ITalkDetailView iTalkDetailView, BaseView baseView)
    {
        this.context = context;
        this.iTalkDetailView = iTalkDetailView;
        this.baseView=baseView;
    }

    /**
     *论坛详情
     */
    public void getBBSDetail()
    {
        baseView.showLoading();
        requstType=1;
        MyOkHttpClient myOkHttpClient = new MyOkHttpClient(context,baseView);
        myOkHttpClient.getbbsDetail(UrlData.BASEURL + UrlData.BBSDETAIL, iTalkDetailView.getbbsId(), iTalkDetailView.getPage() + "", handler);
    }
    /**
     *回复论坛
     */
    public void replyComment()
    {
        try{
            requstType=2;
            String content=iTalkDetailView.getetComment().getText().toString();
            if("".equals(content)){
                Toast.makeText(context,"您还没填写评论内容",Toast.LENGTH_SHORT).show();
            }else{
//                baseView.showLoading();
                MyOkHttpClient myOkHttpClient = new MyOkHttpClient(context,baseView);
                myOkHttpClient.replybbs(UrlData.BASEURL + UrlData.REPLYBBS, SharePrefencesUtil.getUser_id(context),content,iTalkDetailView.getbbsId(), handler);
            }
        }catch (Exception e){
        }
    }
    public TalkDetailBean getData(){
        return talkDetailBean;
    }
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            baseView.hideLoading();
            String result = (String) msg.obj;
            Gson gson = new Gson();
            iTalkDetailView.load();
            try {
                switch (msg.what) {
                    case RESPONSE_SUCCEED:
                        if (requstType==1)
                        {
                            Log.e("loginresult", result);

                            talkDetailBean = gson.fromJson(result, TalkDetailBean.class);
                            if (iTalkDetailView.getPage() == 1) {
                                setHesderData();
                                talkDetailListAdapter=new TalkDetailListAdapter(context,talkDetailBean);
                                iTalkDetailView.getListView().setAdapter(talkDetailListAdapter);
                            } else {
                                talkDetailListAdapter.addData(talkDetailBean);
                            }
                            if (talkDetailBean.getTotal() <= (talkDetailBean.getEach() * iTalkDetailView.getPage())) {
                                iTalkDetailView.getListView().setPullLoadEnable(false);
                            }
                        }else{
                            baseBean=gson.fromJson(result,BaseBean.class);
                            if(0==baseBean.getState()){
                                iTalkDetailView.getetComment().setText("");
                                KeyBoardUtils.closeKeybord(iTalkDetailView.getetComment(), context);
                                getBBSDetail();
                                Toast.makeText(context, "评论成功",Toast.LENGTH_SHORT).show();
                                iTalkDetailView.callback();
                            }else{
                                baseView.showError(baseBean.getMsg());
                            }
                        }
                        break;
                    case RESPONSE_FAILED:
                        baseView.showError(result);
                        break;
                }
            }catch(Exception e){
            }
        }
    };

    private void setHesderData() {
        try{
            iTalkDetailView.getTvTitle().setText(talkDetailBean.getRow().getTitle());
            iTalkDetailView.getTvTime().setText(TimeUtils.getTime_china(Long.parseLong(talkDetailBean.getRow().getTimeline())));
            iTalkDetailView.getTvName().setText(talkDetailBean.getRow().getNickname());
            iTalkDetailView.getTvNum().setText(talkDetailBean.getTotal()+"");
            iTalkDetailView.getTvContent().setText(talkDetailBean.getRow().getContent());
            ImageLoaderUtil.loaderImage(UrlData.IMAGEURL + talkDetailBean.getRow().getIcon(), iTalkDetailView.getRivPhoto(), R.mipmap.photo_default);
        }catch (Exception e){

        }
    }
}